<!--
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 * No deletion without permission, or be held responsible to law.
 * @author XPF
-->
<template>
  <div>
    <BasicTable @register="registerTable">
      <template #tableTitle>
        <Icon :icon="getTitle.icon" class="m-1 pr-1" />
        <span> {{ getTitle.value }} </span>
      </template>
      <template #toolbar>
        <a-button type="primary" @click="handleForm({})" v-auth="'sports:sportsScoreGrade:edit'">
          <Icon icon="fluent:add-12-filled" /> {{ t('新增') }}
        </a-button>
      </template>
      <template #firstColumn="{ record }">
        <a @click="handleForm({ scoreGradeId: record.scoreGradeId })">
          {{ record.indexName }}
        </a>
      </template>
    </BasicTable>
    <InputForm @register="registerDrawer" @success="handleSuccess" />
    <DrawerForm @register="registerCorrelation" @success="handleSuccess" />
  </div>
</template>
<script lang="ts">
  export default defineComponent({
    name: 'ViewsSportsSportsScoreGradeList',
  });
</script>
<script lang="ts" setup>
  import { defineComponent } from 'vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { router } from '/@/router';
  import { Icon } from '/@/components/Icon';
  import { BasicTable, BasicColumn, useTable } from '/@/components/Table';
  import {
    physicalSkillsScoreGradeDelete,
    physicalSkillsScoreGradeListData,
    physicalSkillsScoreGradeSelectData,
  } from '/@/api/physical/physicalSkillsScoreGrade';
  import { useDrawer } from '/@/components/Drawer';
  import { FormProps } from '/@/components/Form';
  import InputForm from './form.vue';
  import DrawerForm from './drawer.vue';
  const { t } = useI18n('physical.physicalSkillsScoreGrade');
  const { showMessage } = useMessage();
  const getTitle = {
    icon: router.currentRoute.value.meta.icon || 'ant-design:book-outlined',
    value: router.currentRoute.value.meta.title || t('单项指标得分对应等级管理'),
  };

  const searchForm: FormProps = {
    baseColProps: { lg: 6, md: 8 },
    labelWidth: 120,
    schemas: [
      {
        label: t('关联单项指标'),
        field: 'termId',
        component: 'Select',
        componentProps: {
          allowClear: true,
          api: async () => {
            let res = await physicalSkillsScoreGradeSelectData({ termType: 1 });
            let data = [];
            res.data.forEach((item, index) => {
              data.push({
                label: item.termName + '（' + '适配年级：' + item.adaptGrade + '）',
                value: item.termId,
              });
            });
            return data;
          },
        },
      },
      {
        label: t('单项指标得分'),
        field: 'singleScore',
        component: 'Input',
      },
      {
        label: t('得分对应等级'),
        field: 'scoreGrade',
        component: 'Select',
        componentProps: {
          dictType: 's_term_score_grade',
          allowClear: true,
        },
      },
    ],
  };

  const tableColumns: BasicColumn[] = [
    {
      title: t('关联单项指标名称'),
      dataIndex: 'sportsPhysicalSkills.indexName',
      key: 'a.index_name',
      // sorter: true,
      width: 330,
      align: 'left',
    },
    {
      title: t('单项指标得分'),
      dataIndex: 'singleScore',
      key: 'a.single_score',
      sorter: true,
      width: 130,
      align: 'center',
    },
    {
      title: t('得分对应等级'),
      dataIndex: 'scoreGrade',
      key: 'a.score_grade',
      // sorter: true,
      width: 130,
      align: 'center',
      dictType: 's_term_score_grade',
    },
    {
      title: t('更新时间'),
      dataIndex: 'updateDate',
      key: 'a.update_date',
      sorter: true,
      width: 130,
      align: 'center',
    },
  ];

  const actionColumn: BasicColumn = {
    width: 160,
    actions: (record: Recordable) => [
      {
        icon: 'clarity:note-edit-line',
        title: t('编辑单项指标得分对应等级'),
        onClick: handleForm.bind(this, { scoreGradeId: record.scoreGradeId }),
        auth: 'sports:sportsScoreGrade:edit',
      },
      {
        icon: 'ant-design:bars-outlined',
        title: t('关联评分'),
        //label: '关联评分',
        onClick: CorrelationScore.bind(this, {
          scoreGradeId: record.scoreGradeId,
          // scoreId: record.scoreId,
          termId: record.sportsPhysicalSkills.termId,
        }),
        auth: 'sports:sportsScoreGrade:edit',
      },
      {
        icon: 'ant-design:delete-outlined',
        color: 'error',
        title: t('删除单项指标得分对应等级'),
        popConfirm: {
          title: t('是否确认删除单项指标得分对应等级'),
          confirm: handleDelete.bind(this, { scoreGradeId: record.scoreGradeId }),
        },
        auth: 'sports:sportsScoreGrade:edit',
      },
    ],
  };

  const [registerDrawer, { openDrawer }] = useDrawer();
  const [registerTable, { reload }] = useTable({
    api: physicalSkillsScoreGradeListData,
    beforeFetch: (params) => {
      return params;
    },
    afterFetch: (res) => {
      res.forEach((item, index) => {
        item.sportsPhysicalSkills.adaptGrade = item.sportsPhysicalSkills.adaptGrade.split(',');
        for (var i = 0; i < item.sportsPhysicalSkills.adaptGrade.length; i++) {
          switch (item.sportsPhysicalSkills.adaptGrade[i]) {
            case '1':
              item.sportsPhysicalSkills.adaptGrade[i] = '一年级';
              break;
            case '2':
              item.sportsPhysicalSkills.adaptGrade[i] = '二年级';
              break;
            case '3':
              item.sportsPhysicalSkills.adaptGrade[i] = '三年级';
              break;
            case '4':
              item.sportsPhysicalSkills.adaptGrade[i] = '四年级';
              break;
            case '5':
              item.sportsPhysicalSkills.adaptGrade[i] = '五年级';
              break;
            case '6':
              item.sportsPhysicalSkills.adaptGrade[i] = '六年级';
              break;
            case '7':
              item.sportsPhysicalSkills.adaptGrade[i] = '七年级';
              break;
            case '8':
              item.sportsPhysicalSkills.adaptGrade[i] = '八年级';
              break;
            case '9':
              item.sportsPhysicalSkills.adaptGrade[i] = '九年级';
              break;
            case '10':
              item.sportsPhysicalSkills.adaptGrade[i] = '高一';
              break;
            case '11':
              item.sportsPhysicalSkills.adaptGrade[i] = '高二';
              break;
          }
        }
        item.sportsPhysicalSkills.indexName =
          item.sportsPhysicalSkills.termName + '（' + item.sportsPhysicalSkills.adaptGrade + '）';
      });
    },
    columns: tableColumns,
    actionColumn: actionColumn,
    formConfig: searchForm,
    showTableSetting: true,
    useSearchForm: true,
    canResize: true,
  });

  const [registerCorrelation, { openDrawer: onCorrelation }] = useDrawer();
  function handleForm(record: Recordable) {
    openDrawer(true, record);
  }

  function CorrelationScore(record: Recordable) {
    onCorrelation(true, record);
  }

  async function handleDelete(record: Recordable) {
    const res = await physicalSkillsScoreGradeDelete(record);
    showMessage(res.message);
    handleSuccess();
  }

  function handleSuccess() {
    reload();
  }
</script>
